
#include "iconanimators.h"

#include <QApplication>
#include <QPixmap>
#include <QIcon>

AbstractIconAnimator::AbstractIconAnimator(
    const unsigned short singleFrameDurationInMilliseconds)
:   QObject()
,   m_index(0)
{
    m_timer.setInterval(singleFrameDurationInMilliseconds);

    connect(&m_timer, SIGNAL(timeout())
        , this, SLOT(me_timer_timeout()));
}

const int AbstractIconAnimator::appendIcon(const QIcon &icon)
{
    m_icons.append(icon);
    return m_icons.size();
}

const int AbstractIconAnimator::appendIcon(const QString &fileName)
{
    m_icons.append(QIcon(fileName));
    return m_icons.size();
}

const int AbstractIconAnimator::appendIcons(const QStringList &fileNames)
{
    foreach(const QString &fileName, fileNames)
        m_icons.append(QIcon(fileName));

    return m_icons.size();
}

void AbstractIconAnimator::me_timer_timeout()
{
    if(m_icons.isEmpty())
        return;

    setIcon(m_icons[m_index]);
    qApp->processEvents();

    m_index = (m_index + 1) % m_icons.size();
}

void AbstractIconAnimator::start()
{
    m_backup = icon();
    m_timer.start();
}

void AbstractIconAnimator::stop()
{
    m_timer.stop();
    setIcon(m_backup);
}

const bool AbstractIconAnimator::isActive() const
{
    return m_timer.isActive();
}




#include <QStandardItem>


StandardItemIconAnimator::StandardItemIconAnimator(
    QStandardItem *item
,   const unsigned short singleFrameDurationInMilliseconds)
:   AbstractIconAnimator(singleFrameDurationInMilliseconds)
,   m_item(item)
{
}

void StandardItemIconAnimator::setIcon(const QIcon &icon) const
{
    if(m_item)
        m_item->setIcon(icon);
}

const QIcon StandardItemIconAnimator::icon() const
{
    if(m_item)
        return m_item->icon();

    return QIcon();
}
